// pages/detail/detail.js
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pid: "",
    goodDetail: [],
    //数据数组
    tmp: [],
    suger: [],
    cream: [],
    //颜色变化标志
    tmpFlag: 0,
    sugerFlag: 0,
    creamFlag: 0,
    desc: [],
    //用户选择
    selectTmp: "冷",
    selectSuger: "全糖",
    selectCream: "默认奶油",
    //数量
    quantity: 1,
    count: 1,
    totalCount: 0,
    //加入购物袋
    countShopBag: null,
    //判断登录标志
    loginFlag: false,
    //获取token
    tokenString: "",
    //判断是否收藏
    likeCode: "",
    likeColorFlag: false,
    searchPid: ""
  },

  //根据pid进行商品详细信息搜索
  searchDetail() {
    wx.request({
      url: 'https://kf.webxyq.com/productDetail',
      method: "GET",
      data: {
        appkey: app.globalData.appkey,
        pid: this.data.pid
      },
      success: res => {
        //处理搜索到的数据
        let tmp = res.data.result[0].tem.split("/");
        let suger = res.data.result[0].sugar.split("/");
        let cream = res.data.result[0].cream.split("/");
        let desc = res.data.result[0].desc.split('\n');
        this.setData({
          goodDetail: res.data.result,
          tmp: tmp,
          suger: suger,
          cream: cream,
          desc: desc
        })
      }
    })
  },

  //当用户进行选择时生效
  tmpTouch(e) {
    //获取用户选择
    let tmpFlag = e.currentTarget.dataset.btnText;
    let tmp = this.data.tmp;
    let selectTmp = tmp[tmpFlag];
    this.setData({
      //赋值颜色变化与选择
      tmpFlag: tmpFlag,
      selectTmp: selectTmp
    })
  },

  sugerTouch(e) {
    //获取用户选择
    let sugerFlag = e.currentTarget.dataset.btnText;
    let suger = this.data.suger;
    let selectSuger = suger[sugerFlag];
    this.setData({
      //赋值颜色变化与选择
      sugerFlag: sugerFlag,
      selectSuger: selectSuger
    })
  },

  creamTouch(e) {
    //获取用户选择
    let creamFlag = e.currentTarget.dataset.btnText;
    let cream = this.data.cream;
    let selectCream = cream[creamFlag];
    this.setData({
      //赋值颜色变化与选择
      creamFlag: creamFlag,
      selectCream: selectCream
    })
  },

  //添加的数量
  onChange(event) {
    let quantity = event.detail;
    this.setData({
      quantity: quantity
    })
  },

  //购物袋图标
  onClickIconShop() {
    let loginFlag = this.data.loginFlag;
    if (loginFlag == true) {
      wx.switchTab({
        url: '../shopbag/shopbag',
      })
    }
    else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },

  //收藏图标
  onClickIconLike() {
    let likeColorFlag = this.data.likeColorFlag;
    let loginFlag = this.data.loginFlag;
    if (likeColorFlag == false && loginFlag == true) {
      wx.request({
        url: 'https://kf.webxyq.com/like',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: {
          appkey: app.globalData.appkey,
          pid: this.data.pid,
          tokenString: this.data.tokenString
        },
        success: res => {
          this.setData({
            likeColorFlag: true
          })
        }
      })
    }
    else if (likeColorFlag == true && loginFlag == true) {
      wx.request({
        url: 'https://kf.webxyq.com/notlike',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: {
          appkey: app.globalData.appkey,
          pid: this.data.pid,
          tokenString: this.data.tokenString
        },
        success: res => {
          this.setData({
            likeColorFlag: false
          })
        }
      })
    }
    else {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },

  //加入购物袋
  onClickButton() {
    let loginFlag = this.data.loginFlag;
    let quantity = this.data.quantity;
    let selectTmp = this.data.selectTmp;
    let selectSuger = this.data.selectSuger;
    let selectCream = this.data.selectCream;
    let rule = selectTmp + '/' + selectSuger + '/' + selectCream;
    if (loginFlag == true) {
      wx.request({
        url: 'https://kf.webxyq.com/addShopcart',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        data: {
          pid: this.data.pid,
          count: quantity,
          rule: rule,
          appkey: app.globalData.appkey,
          tokenString: this.data.tokenString
        },
        success: res => {
          wx.showToast({
            title: res.data.msg,
            icon: "success"
          })
          this.getTotalCount()
        }
      })
    }
    else {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },

  getTotalCount(){
    wx.request({
      url: 'https://kf.webxyq.com/shopcartCount',
      method: "GET",
      data: {
        appkey: app.globalData.appkey,
        tokenString: this.data.tokenString
      },
      success: res =>{
        this.setData({
          totalCount: res.data.result
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取单击商品传来的id
    this.setData({
      pid: options.pid,
    })
    //加载搜索
    this.searchDetail();
    //确认是否登录
    wx.getStorage({
      key: "_token",
      //已登陆
      success: res => {
        this.setData({
          tokenString: res.data,
          loginFlag: true
        });
        // 查询购物袋数量
        this.getTotalCount()
        //判断商品是否收藏
        wx.request({
          url: 'https://kf.webxyq.com/findlike',
          method: 'GET',
          header: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
          },
          data: {
            appkey: app.globalData.appkey,
            pid: this.data.pid,
            tokenString: res.data
          },
          success: res => {
            let search = res.data.result[0];
            console.log(search);
            this.setData({
              searchPid: search
            })
            if (search !== undefined) {
              if (search.pid == this.data.pid) {
                this.setData({
                  likeColorFlag : true
                })
              }
              else{
                this.setData({
                  likeColorFlag : false
                })
              }
            }
          }
        })
      },
      //未登录
      fail: err => {
        this.setData({
          loginFlag: false
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})